//
//  ViewController.m
//  GaKan
//
//  Created by mac on 17/9/7.
//  Copyright © 2017年 mac. All rights reserved.

//头文件包含
#import "ViewController.h"
#import "BaseViewController.h"
#import "optionView.h"
#import "SGFocusImageFrame.h"
#import <WebKit/WebKit.h>
#import "DSToast.h"
#import "logInViewController.h"
#import "signViewController.h"
#import "leaveViewController.h"
#import "timeViewController.h"
#import "reportViewController.h"

//协议包含
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,WKNavigationDelegate,WKUIDelegate,logInDelegate>
//全局变量
{
    UIView *mainView;
    UIView *headView;
    UIView *scrollView;
    optionView *choseview;
    UICollectionView *collectiion;
    UITableView *tab;
    NSArray *optionArry;
    NSArray *htmlTittle;
    NSArray *htmlImage;
    UICollectionView *collectionview;
    NSArray *htmlUrlString;
    NSMutableArray *SGFItemArry;
    NSMutableArray *bannerArray;
}
@end

@implementation ViewController
//主函数
- (void)viewDidLoad {
    [super viewDidLoad];
    
    mainView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    mainView.backgroundColor=COLOR(237, 237, 237, 1.0);
    [self.view addSubview:mainView];
    
    headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 64)];
    headView.backgroundColor=COLOR(13, 153, 255, 1.0);
    [mainView addSubview:headView];
    
    UILabel *Lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 40)];
    Lab.text=@"办公管理系统";
    Lab.font=[UIFont systemFontOfSize:15];
    Lab.textAlignment=NSTextAlignmentCenter;
    Lab.textColor=[UIColor whiteColor];
    [Lab setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    [headView addSubview:Lab];
    
    UIButton *option=[[UIButton alloc]initWithFrame:CGRectMake(20, 25, 35, 30)];
    [option setImage:[UIImage imageNamed:@"11"] forState:UIControlStateNormal];
    [option addTarget:self action:@selector(chosetypeGet) forControlEvents:UIControlEventTouchDown];
    [headView addSubview:option];
    
    //[self GetBanners];
    [self mainUImake];
    
}
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    self.toolbarItems =nil;
//    self.navigationController.toolbar.translucent   = YES;
//    self.navigationController.toolbarHidden         = YES;
//}
//ui构造
-(void)mainUImake{
    //轮播图
    SGFocusImageItem *item1=[[SGFocusImageItem alloc]initWithTitle:@"" image:[UIImage imageNamed:@"Ahri-1920x1200.jpg"] tag:1111111];
    SGFocusImageItem *item2=[[SGFocusImageItem alloc]initWithTitle:@"" image:[UIImage imageNamed:@"blood-moon-diana-wp.jpg"] tag:1111111];
    NSArray *array = [NSArray arrayWithObjects:item1,item2, nil];
    SGFocusImageFrame *topFrame=[[SGFocusImageFrame alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(headView.frame), SCREENWIDTH, SCREENHEIGHT/3-CGRectGetMaxY(headView.frame)+60) delegate:nil focusImageItemsArrray:array];
    topFrame.backgroundColor=[UIColor whiteColor];
    [mainView addSubview:topFrame];
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing=1;
    layout.minimumInteritemSpacing=1;
    //集合视图
    collectionview=[[UICollectionView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topFrame.frame), SCREENWIDTH, ScreenHeight-CGRectGetMaxY(topFrame.frame)) collectionViewLayout:layout];
    
    [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    collectionview.backgroundColor=COLOR(237, 237, 237, 1.0);
    collectionview.delegate=self;
    collectionview.dataSource=self;
    
    [mainView addSubview:collectionview];
    UIView *whitefoot=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(collectionview.frame)-1, SCREENWIDTH, SCREENHEIGHT-CGRectGetMaxY(collectionview.frame))];
    whitefoot.backgroundColor=[UIColor whiteColor];
    [mainView addSubview:whitefoot];
    
    //选项栏
    choseview=[[optionView alloc]initWithFrame:CGRectMake(-SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [choseview.alphaiView addGestureRecognizer:tap];
    [choseview reloadInputViews];
    [mainView addSubview:choseview];
    choseview.delegate=self;
    
    [self makeArry];
    
    tab=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, choseview.whiteView.frame.size.width, choseview.whiteView.frame.size.height)];
    tab.separatorStyle = UITableViewCellSelectionStyleNone;
    tab.dataSource=self;
    tab.delegate=self;
    [choseview.whiteView addSubview:tab];
}
//将要显示
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.toolbarItems =nil;
    self.navigationController.toolbar.translucent   = NO;
    self.navigationController.toolbarHidden         = YES;
    [choseview UIInit];
    NSLog(@"------------------------");
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [choseview.alphaiView addGestureRecognizer:tap];
    [choseview reloadInputViews];
    [mainView addSubview:choseview];
    choseview.delegate=self;
    
    [self makeArry];
    tab=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, choseview.whiteView.frame.size.width, choseview.whiteView.frame.size.height)];
    tab.separatorStyle = UITableViewCellSelectionStyleNone;
    tab.dataSource=self;
    tab.delegate=self;
    [choseview.whiteView addSubview:tab];
}
//数据预设
-(void)makeArry{
    //optionArry=[NSArray arrayWithObjects:@"开通vip",@"领劵中心",@"使用说明",@"激活码通道",@"关于我们",@"设置", nil];
    
    optionArry=[NSArray arrayWithObjects:
                [NSString stringWithFormat:@"部门:%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"user_department"]],
                [NSString stringWithFormat:@"职位:%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"user_job"]],
                [NSString stringWithFormat:@"退出登录"],nil];
    
    htmlTittle=[NSArray arrayWithObjects:@"签到",@"请假",@"考勤",@"日志登记", nil];
    
    htmlImage=[NSArray arrayWithObjects:[UIImage imageNamed:@"4"],[UIImage imageNamed:@"1"],[UIImage imageNamed:@"2"],[UIImage imageNamed:@"3"], nil];
    htmlUrlString=[NSArray arrayWithObjects:@"http://www.youku.com",@"https://v.qq.com",@"http://www.iqiyi.com",@"http://www.mgtv.com",@"http://www.le.com",@"http://www.pptv.com",@"http://122.14.208.193/vip1/",@"", nil];
    
}
//登录
-(void)logInOfdelegater{
    logInViewController *view=[[logInViewController alloc]init];
    [[ViewManager shareInstance].NavigationController pushViewController:view animated:YES];
}
//登出
-(void)logout{
    [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"is_login"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"user_id"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"user_name"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"user_login_id"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"user_login_code"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"user_department"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"user_job"];
    logInViewController *view=[logInViewController new];
    [[ViewManager shareInstance].NavigationController pushViewController:view animated:YES];
}

#pragma mark  - collection列表
//集合视图----协议

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    CGFloat x=(SCREENWIDTH-2)/3;
    return CGSizeMake(x, x);
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}

//Items
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return htmlTittle.count;
}

//定义每个Section的四边间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);//分别为上、左、下、右
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor whiteColor];
    if (indexPath.row==8) {
        return cell;
    } else {
        CGFloat x=cell.frame.size.width;
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(20, 20, x-40, x/2)];
        
        image.image=htmlImage[indexPath.row];
        [cell addSubview:image];
        UILabel *Lab=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(image.frame), x, x-CGRectGetMaxY(image.frame))];
        Lab.text=htmlTittle[indexPath.row];
        Lab.font=[UIFont systemFontOfSize:15];
        Lab.textAlignment=NSTextAlignmentCenter;
        Lab.textColor=[UIColor blackColor];
        [cell addSubview:Lab];
        
        return cell;
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
 
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"is_login"] isEqualToString:@"1"]){
        if (indexPath.item==0) {
            signViewController *view = [[signViewController alloc]init];
            //SingleLocationViewController *view = [[SingleLocationViewController alloc]init];
            [[ViewManager shareInstance].NavigationController pushViewController:view animated:YES];
        }else
            if (indexPath.item==1) {
                leaveViewController *view = [[leaveViewController alloc]init];
                [[ViewManager shareInstance].NavigationController pushViewController:view animated:YES];
            }else
                if (indexPath.item==2) {
                    timeViewController *view = [[timeViewController alloc]init];
                    [[ViewManager shareInstance].NavigationController pushViewController:view animated:YES];
                }else{
                    reportViewController *view = [[reportViewController alloc]init];
                    [[ViewManager shareInstance].NavigationController pushViewController:view animated:YES];
                }
    }else{
        [self logInOfdelegater];
    }
    
    
}
//联网状态检测
-(void)alert{
    UIAlertController *alertcontroller=[UIAlertController alertControllerWithTitle:@"温馨提示" message:@"没有WIFI连接，请您确认是否进入" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancleaction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    UIAlertAction *okaction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
        
    }];
    [alertcontroller addAction:okaction];
    [alertcontroller addAction:cancleaction];
    [self presentViewController:alertcontroller animated:YES completion:nil];
}

#pragma mark  - 选项列表
//选项列表---协议
//Rows
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return optionArry.count;
}
//Section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
}
//眉
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellId=[NSString stringWithFormat:@"cellId_%ld_%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell;
    
    cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor whiteColor];
        //UI
        UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(30, 20, 200, 30)];
        lab.font=[UIFont systemFontOfSize:17];
        lab.textColor=[UIColor blackColor];
        lab.tag=1000000;
        [cell addSubview:lab];
    }
    //数据
    UILabel *lab=[cell viewWithTag:1000000];
    lab.text=optionArry[indexPath.row];
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==(optionArry.count-1)) {
        [self logout];
    }
    
}
#pragma mark  - 平移动画
//平移动画
-(void)chosetypeGet{
    
    [UIView animateWithDuration: 0.35 animations: ^{
        
        choseview.center=CGPointMake(SCREENWIDTH/2, choseview.center.y);
        
    } completion: nil];
    choseview.alphaiView.backgroundColor = [UIColor blackColor];
    choseview.alphaiView.alpha = 0.2;
}
-(void)dismiss{
    choseview.alphaiView.backgroundColor = [UIColor clearColor];
    
    [UIView animateWithDuration: 0.35 animations: ^{
        
        choseview.center=CGPointMake(-SCREENWIDTH/2, choseview.center.y);
        
    } completion: nil];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end

